package com.leantech.android.browser.browser.inject

import android.util.Base64
import android.webkit.WebView
import com.leantech.android.browser.browser.getByteArrayFromAsset

const val boldFontCss = "* {\n" +
        "\tfont-weight:700 !important;\n" +
        "}\n" +
        "a,a * {\n" +
        "\tfont-weight:700 !important;\n" +
        "}\n" +
        "a: visited,a: visited *,a: active,a: active * {\n" +
        "\tfont-weight:700 !important;\n" +
        "}\n" +
        "a: hover,a: hover * {\n" +
        "\tfont-weight:700 !important;\n" +
        "}\n" +
        "input,select,option,button,textarea {\n" +
        "\tfont-weight:700 !important;\n" +
        "}\n" +
        "input: focus,select: focus,option: focus,button: focus,textarea: focus,input: hover,select: hover,option: hover,button: hover,textarea: hover {\n" +
        "\tfont-weight:700 !important;\n" +
        "}\n" +
        "input[type=button]: focus,input[type=submit]: focus,input[type=reset]: focus,input[type=image]: focus, input[type=button]: hover,input[type=submit]: hover,input[type=reset]: hover,input[type=image]: hover {\n" +
        "\tfont-weight:700 !important;\n" +
        "}\n"

fun WebView.injectCss(bytes: ByteArray) {
    try {
        val encoded = Base64.encodeToString(bytes, Base64.NO_WRAP)
        if (encoded.isNullOrBlank()) return
        loadUrl(
            "javascript:(function() {" +
                    "var parent = document.getElementsByTagName('head').item(0);" +
                    "var style = document.createElement('style');" +
                    "style.type = 'text/css';" +
                    "style.innerHTML = window.atob('" + encoded + "');" +
                    "parent.appendChild(style)" +
                    "})()"
        )

    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun WebView.injectCss(css: String) {
    injectCss(css.toByteArray())
}

fun makeCustomBackgroundCss(htmlColor: String): String {
    if (htmlColor.isBlank()) return ""
    if (!htmlColor.startsWith("#")) throw IllegalArgumentException("htmlColor must startWith #")
    return """
* {
    color: #000000!important;
    border-color: #555555 !important;
    background-color: $htmlColor !important;
}
input,select,option,button,textarea {
	border: $htmlColor !important;
	border-color: $htmlColor $htmlColor $htmlColor $htmlColor !important;
}
input: focus,select: focus,option: focus,button: focus,textarea: focus,input: hover,select: hover,option: hover,button: hover,textarea: hover {
	border-color: $htmlColor $htmlColor $htmlColor $htmlColor !important;
}
input[type=button],input[type=submit],input[type=reset],input[type=image] {
	border-color: $htmlColor $htmlColor $htmlColor $htmlColor !important;
}
input[type=button]: focus,input[type=submit]: focus,input[type=reset]: focus,input[type=image]: focus, input[type=button]: hover,input[type=submit]: hover,input[type=reset]: hover,input[type=image]: hover {
	background: $htmlColor !important;
	border-color: $htmlColor $htmlColor $htmlColor $htmlColor !important;
}
        """
}

fun makeCustomTextColorCss(htmlColor: String): String {
    if (htmlColor.isBlank()) return ""
    if (!htmlColor.startsWith("#")) throw IllegalArgumentException("htmlColor must startWith #")
    return """
            * {
                color: $htmlColor !important;
            }
            a, a * { 
                color: $htmlColor !important;
            }
            input,select,option,button,textarea {
                color: $htmlColor !important;
            }
            input: focus,select: focus,option: focus,button: focus,textarea: focus,input: hover,select: hover,option: hover,button: hover,textarea: hover {
                color: $htmlColor !important;
            }
            input[type=button],input[type=submit],input[type=reset],input[type=image] {
                color: $htmlColor !important;
            }
        """
}

fun WebView.getReaderCss(): ByteArray {
    return getByteArrayFromAsset(context, "readerview.css")
}